<?php

session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = mysql_real_escape_string($_SESSION['user']);
$result = mysql_query("SELECT * FROM user WHERE id = $user") or die(mysql_error());
$row = mysql_fetch_array($result);

if($row['type'] != 2)
	header("Location: home.php");

$paypal = mysql_real_escape_string($_POST['paypal']);
$summoner = mysql_real_escape_string($_POST['summoner']);

$region = mysql_real_escape_string($_POST['region']);
$games_needed = mysql_real_escape_string($_POST['games_needed']);
$wins_needed = mysql_real_escape_string($_POST['wins_needed']);
$coaches_needed = mysql_real_escape_string($_POST['coaches_needed']);
$start_tier = mysql_real_escape_string($_POST['start_tier']);
$start_division = mysql_real_escape_string($_POST['start_division']);
$payout = mysql_real_escape_string($_POST['payout']);
$booster_payout = mysql_real_escape_string($_POST['booster_payout']);
$timezone = mysql_real_escape_string($_POST['timezone']);

require_once("unirest/lib/Unirest.php");

$newresponse = Unirest::get(
  "https://teemojson.p.mashape.com/player/".$region."/".str_replace(" ", "", $summoner)."/leagues",
  array(
	"X-Mashape-Authorization" => "l5ekV2qGSVYpEWLviXHg4A6oRHRtqNBa"
  ),
  null
);

$decode = json_decode($newresponse->raw_body)->data->summonerLeagues;

$beginning_lp = 0;
$newwins = 0;
$newlosses = 0;
//echo "<pre>"; var_dump($newresponse); echo "</pre>"; die;
$x = 0;
while(isset($decode->array[$x]))
{
//$newdecode = json_decode($newresponse->raw_body)->data->summonerLeagues->array[0]->entries->array;
$newdecode = $decode->array[$x]->entries->array;
//echo "<pre>"; var_dump($newdecode); echo "</pre>"; die;

$y = 0;
while(isset($newdecode[$y]))
{
	if(strtolower($newdecode[$y]->playerOrTeamName) == strtolower($summoner))
	{
		$newwins = $newdecode[$y]->wins;
		$newlosses = $newdecode[$y]->losses;
		$beginning_lp = $newdecode[$y]->leaguePoints;
		break;
	}

	$y++;
}
$x++;
}

$beginning_stats = $newwins." wins, ".$newlosses." losses";

$result = mysql_query("SELECT * FROM user WHERE paypal = '$paypal'") or die(mysql_error());

if(mysql_num_rows($result) == 0)
{

	$password = substr(md5(rand()), 0, 7);

	$to = $paypal;
	$subject = 'ELO Coach Boosting';
	$message = str_replace("[password]", $password, $signup_message);
	$message = str_replace("\n", "\r\n", $message);

	$headers = 'From: Admin@VeteranBoosters.com' . "\r\n" .
		'Reply-To: Admin@VeteranBoosters.com' . "\r\n" .
		'X-Mailer: PHP/' . phpversion();

	mail($to, $subject, $message, $headers);

	$hash = crypt($password);

	mysql_query("INSERT INTO user (paypal, summoner, username, password, region, type) VALUES ('$paypal', '$summoner', '$summoner', '$hash', '$region', 0)") or die(mysql_error());

	$user = mysql_insert_id();
}
else
{
	$row = mysql_fetch_array($result);
	$user = $row['id'];


	$password = substr(md5(rand()), 0, 7);

	$to = $paypal;
	$subject = 'ELO Coach Boosting';
	$message = str_replace("[password]", $password, $new_order_message);
	$message = str_replace("\n", "\r\n", $message);

	$headers = 'From: Admin@VeteranBoosters.com' . "\r\n" .
		'Reply-To: Admin@VeteranBoosters.com' . "\r\n" .
		'X-Mailer: PHP/' . phpversion();

	mail($to, $subject, $message, $headers);
}

if($games_needed == "")
	$games_needed = 0;
if($wins_needed == "")
	$wins_needed = 0;
if($coaches_needed == "")
	$coaches_needed = 0;

$orders = mysql_query("SELECT MAX(id) FROM order_record");
$order_count = mysql_fetch_array($orders);
$duos = mysql_query("SELECT MAX(id) FROM duo_order");
$duo_count = mysql_fetch_array($duos);
$leveling = mysql_query("SELECT MAX(id) FROM leveling_order");
$leveling_count = mysql_fetch_array($leveling);
$referral = mysql_query("SELECT MAX(id) FROM referral_order");
$referral_count = mysql_fetch_array($referral);
$account = mysql_query("SELECT MAX(id) FROM account_order");
$account_count = mysql_fetch_array($account);

$max_order = $order_count['MAX(id)'];
$max_duo = $duo_count['MAX(id)'];
$max_leveling = $leveling_count['MAX(id)'];
$max_referral = $referral_count['MAX(id)'];
$max_account = $acount_count['MAX(id)'];

$new_id = $max_order;

if($max_duo > $new_id)
	$new_id = $max_duo;
if($max_leveling > $new_id)
	$new_id = $max_leveling;
if($max_referral > $new_id)
	$new_id = $max_referral;
if($max_account > $new_id)
	$new_id = $max_account;


$new_id++;
$all_orders = $new_id;

mysql_query("INSERT INTO duo_order (id, user, number_of_games, number_of_wins, number_of_coaches, start_tier, start_division, payout, booster_payout, summoner, region, timestamp, beginning_stats, timezone, beginning_lp) VALUES ($all_orders, $user, $games_needed, $wins_needed, $coaches_needed, '$start_tier', $start_division, $payout, $booster_payout, '$summoner', '$region', ".time().", '$beginning_stats', '$timezone', $beginning_lp)") or die(mysql_error());

$order = $all_orders;

$result = mysql_query("SELECT * FROM user WHERE type = 2");
$row = mysql_fetch_array($result);
$admin = $row['id'];

if($games_needed != 0)
{
	$duo_per_game = mysql_real_escape_string($duo_per_game);
	mysql_query("INSERT INTO duo_order_chat (user, timestamp, message, duo_order) VALUES ($admin, ".time().", '$duo_per_game', $order)") or error_log(mysql_error());
}
else if($wins_needed != 0)
{
	$duo_per_win = mysql_real_escape_string($duo_per_win);
	mysql_query("INSERT INTO duo_order_chat (user, timestamp, message, duo_order) VALUES ($admin, ".time().", '$duo_per_win', $order)") or error_log(mysql_error());
}
else
{
	$duo_coaching = mysql_real_escape_string($duo_coaching);
	mysql_query("INSERT INTO duo_order_chat (user, timestamp, message, duo_order) VALUES ($admin, ".time().", '$duo_coaching', $order)") or error_log(mysql_error());
}

header("Location: orders-admin.php");

?>